package Utils

import java.awt.*
import java.io.File
import java.net.Socket
import java.util.*
import javax.imageio.ImageIO

class Screenshot
{



companion object
{

    fun getWatermarkLength(waterMarkContent: String, g: Graphics2D): Int {
        return g.getFontMetrics(g.font).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length)
    }


    public  fun screenshot():String
    {

        val d = Toolkit.getDefaultToolkit().screenSize
        val width= d.getWidth().toInt()
        val height =d.getHeight().toInt()
        val screenshot = Robot().createScreenCapture(Rectangle(0, 0, width, height))
        val id = UUID.randomUUID().toString() + ".png"



        val img: Graphics2D = screenshot.createGraphics();
        img.drawImage(screenshot, 0, 0, width, height, null);
        img.setColor(Color.BLUE);

        val x: Int = width - 2 * getWatermarkLength(Date().toString(), img)
        val y: Int = height - 2 * getWatermarkLength(Date().toString(), img)
        img.drawString(Date().toString(), x, y)



        ImageIO.write(screenshot, "png", File(id))

//        val getImageStr = Base64.GetImageStr(id)

//        val inputStream: InputStream = FileInputStream(id)
//        val readBytes = inputStream.readBytes()
//        inputStream.close()
//        Files.delete(Paths.get(id))

        return File(id).absolutePath

    }

    }

}



